﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace DemoExtNet.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSCongViecChinh : BusinessBase
    {
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();

        public void EditCongViecChinh(int hdId, string ten, DateTime ngay, string noiDung, string chucVu, int coQuan, bool flag, string coQuanKhac)
        {
            List<NSCongViecChinh> lst = (List<NSCongViecChinh>)HttpContext.Current.Session["CongViecChinh"];
            NSCongViecChinh pc = lst.Where(a => a.CongViecChinhId.Equals(hdId)).SingleOrDefault();
            pc.ThoiGianThayDoi = ngay;
            pc.GhiChu = noiDung;
            pc.Ten = ten;
            pc.ChucVu = chucVu;
            pc.CoQuanId = CoQuanId;
            pc.ToChucFlag_CongViecChinh = flag;
            if (!flag)
            {
                pc.CoQuanKhac = coQuanKhac;
            }
          
        }
        public void DeleteCongViecChinh(int hdId)
        {
            List<NSCongViecChinh> lst = (List<NSCongViecChinh>)HttpContext.Current.Session["CongViecChinh"];
            NSCongViecChinh pc = lst.Where(a => a.CongViecChinhId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllCongViecChinhByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSCongViecChinhs
                         where a.NhanVienId.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSCongViecChinhs.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateCongViecChinh_NhanVien(int nhanVienId, List<NSCongViecChinh> obj)
        {

            var queryNBLByNhanVienId = (from a in db.NSCongViecChinhs
                                        where a.NhanVienId.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSCongViecChinhs.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSCongViecChinh pc in obj)
            {
                NSCongViecChinh item = new NSCongViecChinh();
                CopyNSCongViecChinh(pc, item);
                db.NSCongViecChinhs.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }
        private void CopyNSCongViecChinh(NSCongViecChinh src, NSCongViecChinh des)
        {
            des.ThoiGianThayDoi = src.ThoiGianThayDoi;
            des.GhiChu = src.GhiChu;
            des.NhanVienId = src.NhanVienId;
            des.Ten = src.Ten;
            des.ChucVu = src.ChucVu;
            des.CoQuanId = src.CoQuanId;
            des.ToChucFlag_CongViecChinh = src.ToChucFlag_CongViecChinh;
            des.CoQuanKhac = src.CoQuanKhac;
        }
        public List<NSCongViecChinh> LoadCongViecChinhByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSCongViecChinhs
                         where a.NhanVienId.Equals(nhanVienId)
                         orderby a.NhanVienId descending
                         select a).ToList();

            return query;

        }
        public NSCongViecChinh GetCongViecChinh(int nhanVienId, string ten, DateTime ngay, string noiDung, string chucVu, int coQuan, bool flag, string coQuanKhac)
        {
            var db = new DataClassesDataContext();
            NSCongViecChinh hd = new NSCongViecChinh();
            hd.CongViecChinhId = 0 - DateTime.Now.Second;
            hd.ThoiGianThayDoi = ngay;
            hd.GhiChu = noiDung;
            hd.NhanVienId = nhanVienId;
            hd.Ten = ten;
            hd.ChucVu = chucVu;
            hd.CoQuanId = coQuan;
            hd.ToChucFlag_CongViecChinh = flag;
            if (!flag)
            {
                hd.CoQuanKhac = coQuanKhac;
            }
          

            return hd;
        }
    }
}